﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pavilion
{
    public class AnimationResource : ResourceBase
    {
        public event Action<Animation> AnimationAdded, AnimationRemoved;

        public string BuiltDataPath { get { return ResourcePath + @"Built Data\"; } }
        public string RawDataPath { get { return ResourcePath + @"Raw Data\"; } }
        public override string ResourcePath { get { return GameEngine.AnimationsDirectory + Name + @"\"; } }
        public Animation this[string animationName] { get { return animationDictionary[animationName]; } }
        public IEnumerable<Animation> AnimationList { get { return animationDictionary.Values; } }
        public ContainerNode RootContainer { get; set; }

        private Dictionary<string, Animation> animationDictionary;

        public AnimationResource(string name)
        {
            base.Name = name;
            animationDictionary = new Dictionary<string, Animation>();
            RootContainer = new ContainerNode("Root");
        }

        public override void Dispose()
        {
            List<Animation> animationList = new List<Animation>();

            foreach (Animation animation in animationList)
                animation.Dispose();

            animationDictionary.Clear();
            animationList.Clear();
            Name = null;
        }

        public void AddAnimationList(IEnumerable<Animation> animationList)
        {
            foreach (Animation animation in animationList)
                AddAnimation(animation);
        }

        public void AddAnimation(Animation animation)
        {
            animationDictionary.Add(animation.Name, animation);

            animation.Disposed += (Animation disposedAnimation) => { animationDictionary.Remove(disposedAnimation.Name); OnAnimationRemoved(disposedAnimation); };
            animation.LoadAllFrames(BuiltDataPath);

            OnAnimationAdded(animation);
        }

        public Animation AddNewAnimation()
        {
            Animation animation = new Animation() { Name = GetUniqueName() };
            AddAnimation(animation);

            return animation;
        }

        public bool Contains(string animationName)
        {
            return animationDictionary.ContainsKey(animationName);
        }

        private string GetUniqueName()
        {
            int count = 0;
            string animationName = "Animation" + count++;

            while (animationDictionary.ContainsKey(animationName))
                animationName = "Animation" + count++;

            return animationName;
        }

        private void OnAnimationAdded(Animation animation)
        {
            if (AnimationAdded != null)
                AnimationAdded(animation);
        }

        private void OnAnimationRemoved(Animation animation)
        {
            if (AnimationRemoved != null)
                AnimationRemoved(animation);
        }
    }
}
